package com.fuyun.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fuyun.common.core.domain.model.LoginUser;
import com.fuyun.common.utils.DateUtils;
import com.fuyun.common.utils.ServletUtils;
import com.fuyun.common.utils.spring.SpringUtils;
import com.fuyun.plan.domain.TPlanProcessingBom;
import com.fuyun.plan.mapper.TPlanProcessingBomMapper;
import com.fuyun.plan.service.ITPlanProcessingBomService;
import com.fuyun.plan.vo.TPlanProcessingBomVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.fuyun.framework.web.service.TokenService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.fuyun.plan.bo.TPlanProcessingAddBo;
import com.fuyun.plan.bo.TPlanProcessingQueryBo;
import com.fuyun.plan.bo.TPlanProcessingEditBo;
import com.fuyun.plan.domain.TPlanProcessing;
import com.fuyun.plan.mapper.TPlanProcessingMapper;
import com.fuyun.plan.vo.TPlanProcessingVo;
import com.fuyun.plan.service.ITPlanProcessingService;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 染色计划Service业务层处理
 *
 * @author Amy
 * @date 2021-05-18
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TPlanProcessingServiceImpl extends ServiceImpl<TPlanProcessingMapper, TPlanProcessing> implements ITPlanProcessingService {

    private final ITPlanProcessingBomService itPlanProcessingBomService;


    @Override
    public TPlanProcessingVo queryById(Long id) {
        TPlanProcessing db = this.baseMapper.selectById(id);
        TPlanProcessingVo tPlanProcessingVo = BeanUtil.toBean(db, TPlanProcessingVo.class);
        if (StringUtils.isNotEmpty(db.getProcCode())) {
            List<TPlanProcessingBomVo> tPlanProcessingBomVos = itPlanProcessingBomService.queryList(db.getProcCode());
            tPlanProcessingVo.setTPlanProcessingBomVos(tPlanProcessingBomVos);
        }
        return tPlanProcessingVo;
    }

    @Override
    public List<TPlanProcessingVo> queryList(TPlanProcessingQueryBo bo) {
        LambdaQueryWrapper<TPlanProcessing> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCreateDate() != null, TPlanProcessing::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getPrepaymentDate() != null, TPlanProcessing::getPrepaymentDate, bo.getPrepaymentDate());
        lqw.eq(StrUtil.isNotBlank(bo.getProcCode()), TPlanProcessing::getProcCode, bo.getProcCode());
        lqw.eq(StrUtil.isNotBlank(bo.getProcType()), TPlanProcessing::getProcType, bo.getProcType());
        lqw.orderByDesc(TPlanProcessing::getId);
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TPlanProcessingVo> entity2Vo(Collection<TPlanProcessing> collection) {
        List<TPlanProcessingVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TPlanProcessingVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TPlanProcessing> page = (Page<TPlanProcessing>) collection;
            Page<TPlanProcessingVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TPlanProcessingAddBo bo) {
// 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
//        获取时间类型随机数
        TPlanProcessing tplanprocessingaddbo = BeanUtil.toBean(bo, TPlanProcessing.class);
        TPlanProcessingBom tplanprocessingbom = BeanUtil.toBean(bo, TPlanProcessingBom.class);
//        validEntityBeforeSave(add);
        tplanprocessingaddbo.setProcType("dyeing");
        tplanprocessingaddbo.setCreateDate(DateUtils.getNowDate());
        tplanprocessingaddbo.setProcStatus("auditNone");
        tplanprocessingaddbo.setReview(loginUser.getUser().getUserName());
        tplanprocessingaddbo.setReviewId(loginUser.getUser().getUserId());
        boolean save = this.save(tplanprocessingaddbo);
        String proCode = DateUtils.getDateNow() + tplanprocessingaddbo.getId();
        tplanprocessingaddbo.setProcCode(proCode);
        this.updateById(tplanprocessingaddbo);

        tplanprocessingbom.setProcCode(proCode);
        boolean saveBom = itPlanProcessingBomService.save(tplanprocessingbom);

        return saveBom & save;
    }

    @Override
    public Boolean updateByEditBo(TPlanProcessingEditBo bo) {
        TPlanProcessing update = BeanUtil.toBean(bo, TPlanProcessing.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TPlanProcessing entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
//        删除子表数据
        List<TPlanProcessing> tPlanProcessings = this.listByIds(ids);
        List<String> proCodes = tPlanProcessings.stream().map(TPlanProcessing::getProcCode).collect(Collectors.toList());
        baseMapper.deleteByProCode(proCodes);


        return this.removeByIds(ids);
    }
}
